﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileFilter.cs" company="">
//   
// </copyright>
// <summary>
//   The file filter.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Core
{
    using System.Text;

    /// <summary>
    ///   The file filter.
    /// </summary>
    public class FileFilter
    {
        #region Public Properties

        /// <summary>
        ///   Gets or sets the mime types.
        /// </summary>
        public MimeType[] MimeTypes { get; set; }

        /// <summary>
        ///   Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The to string.
        /// </summary>
        /// <returns> The <see cref="string" /> . </returns>
        public override string ToString()
        {
            var builder = new StringBuilder();

            if (this.MimeTypes == null || this.MimeTypes.Length == 0)
            {
                builder.Append("*.*");
            }
            else
            {
                var notFirst = false;
                foreach (var mimeType in this.MimeTypes)
                {
                    foreach (var extension in mimeType.FileExtensions)
                    {
                        if (notFirst)
                        {
                            builder.Append(";");
                        }

                        builder.Append("*.");
                        builder.Append(extension);
                        notFirst = true;
                    }
                }
            }

            var extensions = builder.ToString();
            builder.Clear();

            builder.Append(this.Name);
            builder.Append(" (");
            builder.Append(extensions);
            builder.Append(" )");
            builder.Append("|");
            builder.Append(extensions);
            return builder.ToString();
        }

        #endregion
    }
}